package cn.springcloud.api.apiUtils;


import java.util.regex.Pattern;

/**
 *
 * 身份识别正则工具
 */
public class IdentifyUtil {

    /**
     * 手机正则
     **/
    public static final String REGEX_PHONE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    /**
     * qq正则
     **/
    public static final String REGEX_QQ = "^[a-zA-Z0-9_-]{5,19}$";
    /**
     * 微信正则
     **/
    public static final String REGEX_WECHAT = "^[a-zA-Z]{1}[-_a-zA-Z0-9]{5,19}$";
    /**
     * 银行卡正则
     **/
    public static final String REGEX_BANKCARD = "^(\\d{16}|\\d{19})$";
    /**
     * 18位身份证正则
     **/
    public static final String REGEX_IDCARD = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
            "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";

    public static final String REGEX_IMEI = "/d{15}";

    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";


    public static boolean checkIDCard(String IDNumber) {
        boolean matches = IDNumber.matches(REGEX_IDCARD);
        //判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    //前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    //这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        return false;
                    }
                } catch (Exception e) {
                    return false;
                }
            }
        }
        return matches;
    }

    /**
     * 格式化手机号码,去掉非数字
     *
     * @param str
     * @return
     */
    public static String formatPhoneNumber(String str) {
        if (str == null) {
            return "";
        }
        StringBuilder str2 = new StringBuilder();
        str = str.trim();
        if (str != null && !"".equals(str)) {
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                    str2.append(str.charAt(i));
                }
            }
        }
        return str2.toString();
    }

    /**
     * 验证号码有效
     *
     * @param str
     * @return
     */
    boolean validateVictimPhone(String str, String[] regx) {
        if (!StringUtil.isNotBlank(str)) {
            return false;
        } else {
            if (regx.length > 0) {
                for (int i = 0; i < regx.length; i++) {
                    str.equals(regx[i]);
                    return false;
                }
            }
            return false;
        }
    }


    /**
     * 手机
     **/
    public static boolean checkPhone(String str) {
        return Pattern.matches(REGEX_PHONE, str);
    }

    public static boolean formatAndCheckPhone(String str) {
        str = formatPhoneNumber(str);
        return Pattern.matches(REGEX_PHONE, str);
    }


    /**
     * qq
     **/
    public static boolean checkQQ(String str) {
        return Pattern.matches(REGEX_QQ, str);
    }

    /**
     * 微信号
     **/
    public static boolean checkWeChat(String str) {
        return Pattern.matches(REGEX_WECHAT, str);
    }

    /**
     * 银行卡
     **/
    public static boolean checkBankCard(String str) {
        return Pattern.matches(REGEX_BANKCARD, str);
    }

    /**
     * IMEI
     **/
    public static boolean checkIMEI(String str) {
        return Pattern.matches(REGEX_IMEI, str);
    }

    /**
     * Email
     **/
    public static boolean checkEMail(String str) {
        return Pattern.matches(REGEX_EMAIL, str);
    }


}
